package com.welike.shibo.controller.v2.api;

import com.alibaba.fastjson.JSONObject;
import com.welike.shibo.aspect.annotation.AutoLog;
import com.welike.shibo.eneity.JsonResult;
import com.welike.shibo.service.SysRoleService;
import com.welike.shibo.service.SysUserService;
import com.welike.shibo.utils.CommonUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author fangyu
 * @version v1.0.0
 * @since 2019/11/21 6:03 下午
 */
@Slf4j
@Api(value = "权限相关接口")
@RestController
@RequestMapping("/v2/api/user")
public class PermissionController {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysRoleService sysRoleService;

    /**
     * 查询用户列表
     * @param pageNum 页码
     * @param pageSize 页大小
     * @return JsonResult
     */
    @AutoLog(operateState = "管理员列表")
    @RequiresPermissions("user:list")
    @GetMapping("/list")
    public JsonResult listUser(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        return sysUserService.listUser(pageNum, pageSize);
    }

    /**
     * 添加用户
     * @param requestJson 请求
     * @return JsonResult
     */
    @AutoLog(operateState = "添加管理员")
    @RequiresPermissions("user:add")
    @PostMapping("/addUser")
    public JsonResult addUser(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "username, password, nickname, roleId");
        log.info("JSONObject => {},{},{},{}",
                requestJson.getString("username"),
                requestJson.getString("password"),
                requestJson.getString("nickname"),
                requestJson.getString("roleId"));
        return sysUserService.addUser(requestJson);
    }

    /**
     * 删除用户
     * @param requestJson 请求
     * @return JsonResult
     */
    @AutoLog(operateState = "删除管理员")
    @RequiresPermissions("user:delete")
    @PostMapping("/deleteUser")
    public JsonResult deleteUser(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "userId");
        return sysUserService.deleteUser(requestJson);
    }

    /**
     * 用户更新
     * @param requestJson 请求
     * @return JsonResult
     */
    @AutoLog(operateState = "管理员更新")
    @RequiresPermissions("user:update")
    @PostMapping("/updateUser")
    public JsonResult updateUser(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "nickname, roleId, deleteStatus, userId");
        return sysUserService.updateUser(requestJson);
    }


    /**
     * 查询所有角色
     * @return JsonResult
     */
    @AutoLog(operateState = "查询所有角色")
    @RequiresPermissions(value = {"user:add", "user:update"}, logical = Logical.OR)
    @GetMapping("/getAllRoles")
    public JsonResult getAllRoles() {
        return sysRoleService.getAllRoles();
    }

    /**
     * 角色列表
     * @return JsonResult
     */
    @AutoLog(operateState = "角色列表")
    @RequiresPermissions("role:list")
    @GetMapping("/listRole")
    public JsonResult listRole() {
        return sysRoleService.listRole();
    }

    /**
     * 查询所有权限, 给角色分配权限时调用
     * @return JsonResult
     */
    @AutoLog(operateState = "查询所有权限")
    @RequiresPermissions("role:list")
    @GetMapping("/listAllPermission")
    public JsonResult listAllPermission() {
        return sysRoleService.listAllPermission();
    }

    /**
     * 新增角色
     * @param requestJson 请求
     * @return JsonResult
     */
    @AutoLog(operateState = "修改角色")
    @RequiresPermissions("role:add")
    @PostMapping("/addRole")
    public JsonResult addRole(@RequestBody JSONObject requestJson){
        CommonUtil.hasAllRequired(requestJson, "roleName,permissions");
        return sysRoleService.addRole(requestJson);
    }

    /**
     * 修改角色
     * @param requestJson 请求
     * @return JsonResult
     */
    @AutoLog(operateState = "修改角色")
    @RequiresPermissions("role:update")
    @PostMapping("/updateRole")
    public JsonResult updateRole(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "roleId,roleName,permissions");
        return sysRoleService.updateRole(requestJson);
    }

    /**
     * 删除角色
     * @param requestJson 请求
     * @return JsonResult
     */
    @AutoLog(operateState = "删除角色")
    @RequiresPermissions("role:delete")
    @PostMapping("/deleteRole")
    public JsonResult deleteRole(@RequestBody JSONObject requestJson){
        CommonUtil.hasAllRequired(requestJson, "roleId");
        return sysRoleService.deleteRole(requestJson);
    }
}
